package avicit.bdp.dds.dao.mapper;

import avicit.bdp.common.base.BaseDAO;
import avicit.bdp.dds.dispatch.enums.ExecutionStatus;
import avicit.bdp.dds.api.dto.StateDto;
import avicit.bdp.dds.dao.entity.ExecuteStatusCount;
import avicit.bdp.dds.dao.entity.ProcessInstance;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * process instance mapper interface
 */
public interface ProcessInstanceMapper extends BaseDAO<ProcessInstance> {

    /**
     * query process instance detail info by id
     *
     * @param processId processId
     * @return process instance
     */
    ProcessInstance queryDetailById(@Param("processId") String processId);

    /**
     * query process instance by host and stateArray
     *
     * @param host       host
     * @param stateArray stateArray
     * @return process instance list
     */
    List<ProcessInstance> queryByHostAndStatus(@Param("host") String host,
                                               @Param("states") int[] stateArray);

    /**
     * query process instance by tenantId and stateArray
     *
     * @param tenantId tenantId
     * @param states   states array
     * @return process instance list
     */
    List<ProcessInstance> queryByTenantIdAndStatus(@Param("tenantId") int tenantId,
                                                   @Param("states") int[] states);

    /**
     * query process instance by worker group and stateArray
     *
     * @param workerGroupId workerGroupId
     * @param states        states array
     * @return process instance list
     */
    List<ProcessInstance> queryByWorkerGroupIdAndStatus(@Param("workerGroupId") int workerGroupId,
                                                        @Param("states") int[] states);

    /**
     * process instance page
     *
     * @param processDefinitionId processDefinitionId
     * @param searchVal           searchVal
     * @param executorId          executorId
     * @param statusArray         statusArray
     * @param host                host
     * @param startTime           startTime
     * @param endTime             endTime
     * @return process instance page
     */
    Page<ProcessInstance> queryProcessInstanceListPaging(@Param("processDefinitionId") String processDefinitionId,
                                                         @Param("searchVal") String searchVal,
                                                         @Param("executorId") Integer executorId,
                                                         @Param("states") int[] statusArray,
                                                         @Param("host") String host,
                                                         @Param("startTime") Date startTime,
                                                         @Param("endTime") Date endTime);

    /**
     * set failover by host and state array
     *
     * @param host       host
     * @param stateArray stateArray
     * @return set result
     */
    int setFailoverByHostAndStateArray(@Param("host") String host,
                                       @Param("states") int[] stateArray);

    /**
     * update process instance by state
     *
     * @param originState originState
     * @param destState   destState
     * @return update result
     */
    int updateProcessInstanceByState(@Param("originState") ExecutionStatus originState,
                                     @Param("destState") ExecutionStatus destState);

    /**
     * update process instance by tenantId
     *
     * @param originTenantId originTenantId
     * @param destTenantId   destTenantId
     * @return update result
     */
    int updateProcessInstanceByTenantId(@Param("originTenantId") int originTenantId,
                                        @Param("destTenantId") int destTenantId);

    /**
     * update process instance by worker groupId
     *
     * @param originWorkerGroupId originWorkerGroupId
     * @param destWorkerGroupId   destWorkerGroupId
     * @return update result
     */
    int updateProcessInstanceByWorkerGroupId(@Param("originWorkerGroupId") int originWorkerGroupId, @Param("destWorkerGroupId") int destWorkerGroupId);

    /**
     * count process instance state by user
     *
     * @param startTime  startTime
     * @param endTime    endTime
     * @param projectIds projectIds
     * @return ExecuteStatusCount list
     */
    List<ExecuteStatusCount> countInstanceStateByUser(
            @Param("startTime") Date startTime,
            @Param("endTime") Date endTime,
            @Param("projectIds") Integer[] projectIds);

    /**
     * query process instance by processDefinitionId
     *
     * @param processDefinitionId processDefinitionId
     * @param size                size
     * @return process instance list
     */
    List<ProcessInstance> queryByProcessDefineId(
            @Param("processDefinitionId") String processDefinitionId,
            @Param("size") int size);

    /**
     * query last scheduler process instance
     *
     * @param definitionId processDefinitionId
     * @param startTime    startTime
     * @param endTime      endTime
     * @return process instance
     */
    ProcessInstance queryLastSchedulerProcess(@Param("processDefinitionId") String definitionId,
                                              @Param("startTime") Date startTime,
                                              @Param("endTime") Date endTime);

    /**
     * query last running process instance
     *
     * @param definitionId definitionId
     * @param startTime    startTime
     * @param endTime      endTime
     * @param stateArray   stateArray
     * @return process instance
     */
    ProcessInstance queryLastRunningProcess(@Param("processDefinitionId") String definitionId,
                                            @Param("startTime") Date startTime,
                                            @Param("endTime") Date endTime,
                                            @Param("states") int[] stateArray);

    /**
     * query last manual process instance
     *
     * @param definitionId definitionId
     * @param startTime    startTime
     * @param endTime      endTime
     * @return process instance
     */
    ProcessInstance queryLastManualProcess(@Param("processDefinitionId") String definitionId,
                                           @Param("startTime") Date startTime,
                                           @Param("endTime") Date endTime);

    /**
     * 查询最新正在运行的流程实例
     *
     * @param definitionId 流程实例ID
     * @param state        运行状态
     * @return 正在运行的最新的流程实例
     */
    ProcessInstance queryRunningProcess(@Param("processDefinitionId") String definitionId,
                                        @Param("state") ExecutionStatus state);

    /**
     * 查询监控列表
     *
     * @param projectId      工程ID
     * @param definitionId   流程定义ID
     * @param instanceName   流程实例名称
     * @param startTime      开始时间
     * @param endTime        结束时间
     * @param instanceType   流程实例类型
     * @param statusList 实例状态
     * @return 列表
     */
    Page<ProcessInstance> getListForControl(@Param("projectId") String projectId,
                                            @Param("definitionId") String definitionId,
                                            @Param("instanceName") String instanceName,
                                            @Param("startTime") String startTime, @Param("endTime") String endTime,
                                            @Param("instanceType") Integer instanceType,
                                            @Param("statusList") List<Integer> statusList);

    /**
     * getListForTotalControl
     *
     * @param instanceName   流程实例名称
     * @param startTime      开始时间
     * @param endTime        结束时间
     * @param statusList 实例状态
     * @return 列表
     */
    Page<ProcessInstance> getListForTotalControl(@Param("projectId") String projectId,
                                                 @Param("instanceName") String instanceName,
                                                 @Param("startTime") String startTime,
                                                 @Param("endTime") String endTime,
                                                 @Param("type") Integer type,
                                                 @Param("statusList") List<Integer> statusList);

    /**
     * 获取各个流程成功实例数
     *
     * @param yesterday 昨日
     * @param state 状态
     * @return 各个流程成功实例数
     */
    List<StateDto> getDataCount(@Param("yesterday") String yesterday,
                                @Param("state") Integer state);

    List<StateDto> getTodayDataAll(@Param("today") String today,
                                   @Param("state") Integer state,
                                   @Param("projectIdList") List<String> projectIdList);

    List<StateDto> getTodayDataByProjectId(@Param("today") String today,
                                           @Param("state") Integer state,
                                           @Param("projectId") String projectId);

    List<Map<String, Object>> getProcessPieAll(@Param("today") String today,
                                               @Param("projectIdList") List<String> projectIdList);

    List<Map<String, Object>> getProcessPieAllByProjectId(@Param("today") String today,
                                                          @Param("projectId") String projectId);

    Long getDataCountByState(@Param("today") String today,
                             @Param("projectIdList") List<String> projectIdList,
                             @Param("state") Integer state);

    Long getSlowCountByState(@Param("today") String today,
                             @Param("projectIdList") List<String> projectIdList,
                             @Param("state") Integer state,
                             @Param("slowTime") Long slowTime);

    Long getDataCountByProjectId(@Param("today") String today,
                                 @Param("projectId") String projectId,
                                 @Param("state") Integer state);

    Long getSlowCountByProjectId(@Param("today") String today,
                                 @Param("projectId") String projectId,
                                 @Param("state") Integer state,
                                 @Param("slowTime") Long slowTime);

    ProcessInstance queryLastOneProcess(@Param("processDefinitionId") String processDefinitionId);

    List<ProcessInstance> queryByProcessDefineIds(@Param("definitionIdList") List<String> definitionIdList);

    ProcessInstance getProcessInstanceByTaskId(@Param("taskId") String taskId);

    List<ProcessInstance> queryByCommandStartTime(@Param("processDefinitionId") String processDefinitionId, @Param("startTime") String startTime);

    ProcessInstance getInstanceStateById(@Param("id")String id);

    List<ProcessInstance> getPhmTrainInstanceList(@Param("processDefinitionId") String processDefinitionId);

    ProcessInstance getLastProcessInstance(@Param("processDefinitionId") String processDefinitionId);

    List<ProcessInstance> getInstanceListByStartTime(@Param("startDate") String startDate);

    List<ProcessInstance> getProcessInstanceByProcessDefinitionId(@Param("processDefinitionId") String processDefinitionId);

    String getFLowTypeByProcessId(@Param("processId")String processId);
}
